package com.xiaoshuidi.cloud.module.rooms.framework.security;

/**
 *
 * @description 用户信息，万能接口
 * @author WangZiYang Ivan.wu
 * @date 2022年5月3日 上午12:41:21
 */
public class SecurityUser {

    private static final ThreadLocal<SecurityUser> SECURITY_USER_THREAD_LOCAL = new ThreadLocal<>();

    // 登录用户id
    private Long userId;
    // 主账号id
    private Long tenantId;


    private static SecurityUser getInstance(){
        return SECURITY_USER_THREAD_LOCAL.get();
    }

    public void setContext(SecurityUser context){
        SECURITY_USER_THREAD_LOCAL.set(context);
    }

    public static void clean(){
        SECURITY_USER_THREAD_LOCAL.remove();
    }

    public static Long getUserId(){
        return SecurityUser.getInstance().userId;
    }

    public static Long getTenantId(){
        return SecurityUser.getInstance().tenantId;
    }


    private SecurityUser(SecurityUserBuild securityUserBuild){
        this.userId = securityUserBuild.userId;
        this.tenantId = securityUserBuild.tenantId;
        setContext(this);
    }

    public static class SecurityUserBuild{
        private Long userId;

        private Long tenantId;

        private String username;

        public SecurityUserBuild userId(Long userId){
            this.userId = userId;
            return this;
        }

        public SecurityUserBuild tenantId(Long tenantId){
            this.tenantId = tenantId;
            return this;
        }

        public SecurityUserBuild username(String username){
            this.username = username;
            return this;
        }

        public SecurityUser bulid(){
            return new SecurityUser(this);
        }
    }

}
